package com.ewanghuiju.app.presenter.pintuan;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.watermail.WaterShopDetailsContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.WaterShopResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 水滴商品详情
 * @time 2020/2/16
 */
public class WaterShopDetailsPresenter extends RxPresenter<WaterShopDetailsContract.View> implements WaterShopDetailsContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public WaterShopDetailsPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData() {

    }

    @Override
    public void attachView(WaterShopDetailsContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 刷新商品信息
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_GET_WATER_SHOP_INFO;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        if (mView != null) {
                            mView.refreshShopInfo();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }

    /**
     * 获取商品详情
     *
     * @param goodsid
     */
    @Override
    public void getShopInfo(String goodsid) {
        addSubscribe(mDataManager.getJfgoodsinfo(goodsid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<WaterShopResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<WaterShopResponBean> optional) {
                        if (mView != null)
                            mView.showShopInfo(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }
//    @Override
//    public void getGoodsspecsData(String goodsid) {
//        addSubscribe(mDataManager.getPtigoodsSpecs(goodsid)
//                        .compose(RxUtil.rxSchedulerHelper())
//                        .compose(RxUtil.handleMyResultCannull())
//                        .subscribeWith(new CommonSubscriber<Optional<List<PintuanGoodsSpecsResponBean>>>(mView) {
//                            @Override
//                            public void onNext(Optional<List<PintuanGoodsSpecsResponBean>> optional) {
//                                if (mView != null)
//                                    mView.showGoodsspecsData(optional.getIncludeNull());
//                            }
//
//                            @Override
//                            public void onError(Throwable e) {
//                                super.onError(e);
////                                if(mView!=null)
////                                    mView.showContentError();
//                            }
//                        })
//        );
//    }
//    /**
//     * 确认订单
//     *
//     * @param goodsid
//     * @param specsid
//     */
//    @Override
//    public void pticonfirmationOrder(String goodsid, String specsid) {
//        addSubscribe(mDataManager.pticonfirmationOrder(goodsid, specsid)
//                .compose(RxUtil.rxSchedulerHelper())
//                .compose(RxUtil.handleMyResultCannull())
//                .subscribeWith(new CommonSubscriber<Optional<AddressListResponBean>>(mView) {
//                    @Override
//                    public void onNext(Optional<AddressListResponBean> optional) {
//                        if (mView != null)
//                            mView.showPticonfirmationOrder(optional.getIncludeNull(), specsid);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                    }
//                })
//        );
//    }

}
